package com.dongjiantong.app.controller.farmrecords.cowRecords;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.farmrecords.model.FarmRecordsDiagnosis;
import com.dongjiantong.farmrecords.service.FarmRecordsDiagnosisService;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 牛档案-诊疗记录
 */

@Slf4j
@OpenBean
public class FarmRecordsBullDiagnosisController {

    @Reference(version = "1.0.0")
    public FarmRecordsDiagnosisService farmRecordsDiagnosisService;

    /**
     * 根据关联牛档案id 查看诊疗记录
     * @param cowRecordId
     * @return
     */
    @OpenMethod(key = "app.farmrcords.farmrecordspollution.bull.selectBullImmunization",
            paramNames = "cowRecordId",
            httpMethods = RequestMethod.GET)
    public Response<List<FarmRecordsDiagnosis>> selectBullDiagnosis(Long cowRecordId) {
        if (cowRecordId == null) { throw new OPServerException("关联牛档案id不能为空"); }

        List<FarmRecordsDiagnosis> farmRecordsDiagnosisList = farmRecordsDiagnosisService.selectBullDiagnosis(cowRecordId);

        return Response.ok(farmRecordsDiagnosisList);
    }

    /**
     * 新增牛档案诊疗记录
     * @param farmRecordsDiagnosis
     * @param toStatus 单据改变为状态
     * @return long 返回插入数据的id
     */
    @OpenMethod(key = "app.farmrecords.farmrecordspollution.bull.createOne",
            paramNames = {"farmRecordsDiagnosis","toStatus"},
            httpMethods = RequestMethod.POST)
    public Response<Map<String,Object>> createOne(@RequestBody(required = false) FarmRecordsDiagnosis farmRecordsDiagnosis,
                                    @RequestParam(required = false) Integer toStatus){
        if (farmRecordsDiagnosis == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST,"请求参数对象不能为空");
        }
        if (farmRecordsDiagnosisService == null){
            throw new OPServerException("farmRecordsDiagnosisService 为null,请检查");
        }

        LoginInfo loginInfo = UserUtil.getCurrentUser();

        Long id = farmRecordsDiagnosisService.createBullOne(loginInfo,farmRecordsDiagnosis, toStatus);

        Map<String,Object> result = Maps.newHashMap();
        result.put("receiptId",id);
        return Response.ok(result);
    }

    /**
     * 根据id或者number 更新一条单据信息
     * @param farmRecordsDiagnosis 请求的对象
     * @return  boolean (更新成功返回true 反之返回false)
     */
    @OpenMethod(key = "app.farmrecords.farmrecordspollution.bull.updateByIdOrNumber",
            paramNames = {"farmRecordsDiagnosis","toStatus"},
            httpMethods = RequestMethod.POST)
    public Response<Boolean> updateByIdOrNumber(@RequestBody (required = false) FarmRecordsDiagnosis farmRecordsDiagnosis,
                                                @RequestParam(required = false) Integer toStatus){
        if (farmRecordsDiagnosis == null){
            throw new OPServerException(HttpCode.BAD_REQUEST,"请求参数对象不能为空");
        }else{
            if (farmRecordsDiagnosisService == null){ throw new OPServerException("farmRecordsDiagnosisService 为null,请检查"); }
            if (farmRecordsDiagnosis.getId() == null){
                throw new OPServerException(HttpCode.PRIMARY_KEY_ID_IS_NOT_EXIST,HttpCode.PRIMARY_KEY_ID_IS_NOT_EXIST.getMessage());
            }else {
                Integer con = farmRecordsDiagnosisService.updateByIdOrNumber(farmRecordsDiagnosis,toStatus);
                return Response.ok(con == 0 ? false : true);
            }
        }
    }
}
